package com.leetcode.problems013;


import java.util.HashMap;

/**
 * @Description https://leetcode-cn.com/problems/string-to-integer-atoi/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {
    static int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    static String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    private static HashMap<String,Integer> symbolMap = new HashMap<>();
    static {
        for (int i = 0; i < symbols.length ; i++) {
            symbolMap.put(symbols[i],values[i]);
        }
    }
    public int romanToInt(String roman) {
        int res = 0;
        for (int i = 0; i < roman.length(); ) {
            StringBuilder temp = new StringBuilder();
            temp.append(roman.charAt(i));
            if(i + 1 < roman.length()){
                temp.append(roman.charAt(i + 1));
            }

            Integer map = symbolMap.get(temp.toString());
            if(map != null){
                res = res + map;
                i = i + 2;
            }else{
                //String key = ;
                res = res + symbolMap.get(String.valueOf(roman.charAt(i)));
                i = i + 1;
            }
        }
        return res;
    }
    public static void main(String[] args) {
        System.out.println(new Solution().romanToInt("MCMXCIV"));
    }
}
